package chapter6.meine.exercises;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import chapter5.StatCalc;

public class StatCalcPanel extends JPanel implements ActionListener{
	
	JLabel title, numEntries, sumLbl, averageLbl, stDeviationLbl;
	JPanel buttonPanel;
	JTextField textInput;
	JButton returnButt, clearButt;
	   private int count;   // Number of numbers that have been entered.
	   private double sum;  // The sum of all the items that have been entered.
	   private double squareSum;  // The sum of the squares of all the items.
	   private double stDev, valueEntered;// Standard deviation and entered value
	
	public StatCalcPanel(){
		setBackground(Color.lightGray);
		setBorder(BorderFactory.createLineBorder(Color.BLUE, 2));
		setLayout(new GridLayout(6, 1));
		
		title = new JLabel("Enter number, press return OR a \"Enter\" button");
		
		buttonPanel = new JPanel();  // constructing panel with buttons
		buttonPanel.setLayout(new FlowLayout());
		buttonPanel.setOpaque(false);
		
		textInput = new JTextField(20);
		textInput.addActionListener(this);
		returnButt = new JButton("Enter");
		returnButt.addActionListener(this);
		clearButt = new JButton("Clear");
		clearButt.addActionListener(this);
		buttonPanel.add(textInput);
		buttonPanel.add(returnButt);
		buttonPanel.add(clearButt); // end of constructing buttPanel
		
		numEntries =     new JLabel("Number of entries:     ");
		sumLbl =         new JLabel("Sum:                   ");
		averageLbl =     new JLabel("Average:               "+ calc.getMean());
		stDeviationLbl = new JLabel("Standart deviation:    "+ calc.getStandardDeviation());
		
		add(title);
		add(buttonPanel);
		add(numEntries);
		add(sumLbl);
		add(averageLbl);
		add(stDeviationLbl);
		
		
		
	}// end of constructor
	
	StatCalc calc = new StatCalc();
	
	

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand() == "Enter" || e.getSource()==textInput){
			try{
			valueEntered = Double.parseDouble(textInput.getText());
			}
			catch (NumberFormatException ex) {
	            // The string xStr is not a legal number.
	            title.setText("Illegal number entered. Only numbers allowed!");
	            return;
	         }
			calc.enter(valueEntered);
			count = calc.getCount();
			numEntries.setText("Number of entries:     "+calc.getCount());
			sumLbl.setText("Sum:                   "+calc.getSum());
			averageLbl.setText("Average:               "+calc.getMean());
			stDeviationLbl.setText("Standart deviation:    "+calc.getStandardDeviation());

			
		}
		else if (e.getActionCommand() == "Clear"){
			calc = new StatCalc();
			title.setText("Enter number, press return OR a \"Enter\" button");
			textInput.setText("0");
			numEntries.setText("Number of entries:     "+calc.getCount());
			sumLbl.setText("Sum:                   "+calc.getSum());
			averageLbl.setText("Average:               "+calc.getMean());
			stDeviationLbl.setText("Standart deviation:    "+calc.getStandardDeviation());
		}
		
	}
	
	

}// end of class StatCalcPanel
